package com.example.videosounddemo

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.view.KeyEvent
import android.widget.TextView
import com.example.videosounddemo.utils.VolumeChangeHelper
import com.example.videosounddemo.utils.VolumeChangeHelper.VolumeChangeListener

class MainActivity : AppCompatActivity() , VolumeChangeListener{
    private lateinit var volumeHelper: VolumeChangeHelper
    private val tag:String = "MainLog"
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        volumeHelper = VolumeChangeHelper
        initView()
    }

    private fun initView() {
      val textView:TextView =   findViewById(R.id.tv_test)
        textView.setOnClickListener {
            if(it.isPressed){
                Log.d(tag,"---按钮状态是按下---" + it.isPressed)
            }else{
                Log.d(tag,"---按钮状态不是按下---" + it.isPressed)
            }
        }
    }

    override fun onResume() {
        super.onResume()
        volumeHelper.registerVolumeChangeListener(this@MainActivity)
    }

    override fun onStop() {
        super.onStop()
        volumeHelper.unRegisterReceiver(this@MainActivity)
    }

    override fun onVolumeDownToMin(volume:Int) {
        Log.d(VolumeChangeHelper.MyVolumeReceiver.TAG_VOLUME, "VolumeKey----> 关闭了音量调节$volume")
    }

    override fun onVolumeUp(volume: Int) {
        Log.d(VolumeChangeHelper.MyVolumeReceiver.TAG_VOLUME, "VolumeKey----> 打开了音量调节$volume")
    }

    override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
        when(keyCode){
            KeyEvent.KEYCODE_VOLUME_UP -> {
                //音量增加
                setVolume(1f,1f)
            }
            KeyEvent.KEYCODE_VOLUME_DOWN ->{
                //音量减小
                setVolume(0f,0f)
            }
        }
        return super.onKeyDown(keyCode, event)
    }

    /**
     * 设置是否增加音量
     */
    private fun setVolume(leftVolume: Float, rightVolume: Float) {

    }
}